package com.book.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.book.bean.Book;
import com.book.bean.CarGood;
import com.book.biz.BookBiz;
import com.book.biz.impl.BookBizImpl;

public class DeleteCarGoodServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		int bid = Integer.parseInt(request.getParameter("bid"));
		BookBiz bookBiz = new BookBizImpl();
		Book book = bookBiz.getOneBook(bid);
		Map<Integer, CarGood> map = new HashMap<Integer, CarGood>();
		HttpSession session = request.getSession();
		Object obj = session.getAttribute("map");
		if(obj!=null){
			map = (Map<Integer, CarGood>) obj;	
		}
		if(map.containsKey(bid)){
			CarGood cg = map.get(bid);
			if(cg.getNum()>1){
				cg.setNum(cg.getNum()-1);
			}else if(cg.getNum()==1){
				map.remove(bid);
			}
			out.print("<script>alert('成功删除');location='carGood.jsp';</script>");
		}else{
			out.print("<script>alert('该图书不存在');location='carGood.jsp';</script>");
		}
		
		
	}

}
